package hankz.spring.context.support;

import hankz.spring.beans.factory.support.DefaultListableBeanFactory;
import hankz.spring.beans.factory.ConfigurableListableBeanFactory;

/**
 * @description: TODO
 * @author: zzc
 * @modified By: zzc
 * @date: Created in 2021/11/21 18:13
 * @version:v1.0 获取Bean工厂和加载资源
 */
public abstract class AbstractRefreshAbleApplicationContext extends AbstractApplicationContext {

    private DefaultListableBeanFactory beanFactory;

    /**
     * <p>获取了 DefaultListableBeanFactory 的实例化以及对资源配置的加载操作 loadBeanDefinitions(beanFactory)，
     * 在加载完成后即可完成对 spring.xml 配置文件中 Bean 对象的定义和注册，
     * 同时也包括实现了接口 BeanFactoryPostProcessor、BeanPostProcessor 的配置 Bean 信息。
     * 但此时资源加载还只是定义了一个抽象类方法 loadBeanDefinitions(DefaultListableBeanFactory beanFactory)，
     * 继续由其他抽象类继承实现</p>
     */
    @Override
    protected void refreshBeanFactory() {
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        loadBeanDefinitions(beanFactory);
        this.beanFactory = beanFactory;
    }

    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);

    private DefaultListableBeanFactory createBeanFactory() {
        return new DefaultListableBeanFactory();
    }

    @Override
    protected ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }

}
